<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="存货名称" prop="inventoryName">
        <el-input
          v-model="queryParams.inventoryName"
          placeholder="请输入存货名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存货编号" prop="inventoryNumber">
        <el-input
          v-model="queryParams.inventoryNumber"
          placeholder="请输入存货编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['inventory:management:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['inventory:management:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['inventory:management:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['inventory:management:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="managementList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="存货名称" align="center" prop="inventoryName" />
      <el-table-column label="存货编号" align="center" prop="inventoryNumber" />
      <el-table-column
        label="规格型号"
        align="center"
        prop="specificationModel"
      />
      <el-table-column
        label="存货类型ID"
        align="center"
        prop="inventoryTypeId"
      />
      <el-table-column
        label="存货分类ID"
        align="center"
        prop="inventoryCategoryId"
      />
      <el-table-column
        label="计价方式ID"
        align="center"
        prop="pricingMethodId"
      />
      <el-table-column label="0启用 1禁用" align="center" prop="status" />
      <el-table-column label="商品用途" align="center" prop="productPurpose" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column
        label="是否支持多计量单位"
        align="center"
        prop="isSupportMultipleUnits"
      />
      <el-table-column label="计量单位ID" align="center" prop="unitId" />
      <el-table-column
        label="采购单位ID"
        align="center"
        prop="purchaseUnitId"
      />
      <el-table-column label="销售单位ID" align="center" prop="salesUnitId" />
      <el-table-column label="库存单位ID" align="center" prop="stockUnitId" />
      <el-table-column label="最低库存" align="center" prop="minStock" />
      <el-table-column label="最高库存" align="center" prop="maxStock" />
      <el-table-column label="安全库存" align="center" prop="safetyStock" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['inventory:management:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['inventory:management:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改存货管理对话框 -->
    <!-- 添加或修改存货管理对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form
        ref="managementRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <!-- 基本信息 -->
        <div class="sub-title">基本信息</div>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="存货编码" prop="inventoryNumber">
              <el-input v-model="form.inventoryNumber" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存货名称" prop="inventoryName">
              <el-input v-model="form.inventoryName" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="规格型号" prop="specificationModel">
              <el-input
                v-model="form.specificationModel"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存货类别" prop="inventoryTypeId">
              <el-select
                v-model="form.inventoryTypeId"
                placeholder="请选择存货类别"
                style="width: 100%"
              >
                <el-option
                  v-for="item in inventoryTypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="存货分类" prop="inventoryCategoryId">
              <el-select
                v-model="form.inventoryCategoryId"
                placeholder="请选择存货分类"
                style="width: 100%"
              >
                <el-option
                  v-for="item in inventoryCategoryList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计价方式" prop="pricingMethodId">
              <el-select
                v-model="form.pricingMethodId"
                placeholder="请选择计价方式"
                style="width: 100%"
              >
                <el-option
                  v-for="item in pricingMethodList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :label="0">启用</el-radio>
                <el-radio :label="1">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="多计量单位" prop="isSupportMultipleUnits">
              <el-radio-group v-model="form.isSupportMultipleUnits">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="商品用途" prop="productPurpose">
              <el-radio-group v-model="form.productPurpose">
                <el-radio label="开票商品">开票商品</el-radio>
                <el-radio label="非开票商品">非开票商品</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input
                v-model="form.remarks"
                type="textarea"
                placeholder="请输入备注内容"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 存放单位 -->
        <div class="sub-title">存放单位</div>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="计量单位" prop="unitId">
              <el-select
                v-model="form.unitId"
                placeholder="请选择计量单位"
                style="width: 100%"
              >
                <el-option
                  v-for="item in inventoryUnitList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购单位" prop="purchaseUnitId">
              <el-select
                v-model="form.purchaseUnitId"
                placeholder="请选择采购单位"
                style="width: 100%"
              >
                <el-option
                  v-for="item in inventoryUnitList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="销售单位" prop="salesUnitId">
              <el-select
                v-model="form.salesUnitId"
                placeholder="请选择销售单位"
                style="width: 100%"
              >
                <el-option
                  v-for="item in inventoryUnitList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库存单位" prop="stockUnitId">
              <el-select
                v-model="form.stockUnitId"
                placeholder="请选择库存单位"
                style="width: 100%"
              >
                <el-option
                  v-for="item in inventoryUnitList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="采购价" prop="purchasePrice">
              <el-input v-model="form.purchasePrice" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标准价" prop="costPrice">
              <el-input v-model="form.costPrice" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="销售价" prop="salesPrice">
              <el-input v-model="form.salesPrice" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="最低库存" prop="minStock">
              <el-input v-model="form.minStock" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最高库存" prop="maxStock">
              <el-input v-model="form.maxStock" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="安全库存" prop="safetyStock">
              <el-input v-model="form.safetyStock" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Management">
import {
  listManagement,
  getManagement,
  delManagement,
  addManagement,
  updateManagement,
  getSelectAllData,
} from "@/api/inventory/management";
import { ref } from "vue";

const { proxy } = getCurrentInstance();

const managementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inventoryName: null,
    inventoryNumber: null,
  },
  rules: {
    inventoryName: [
      { required: true, message: "存货名称不能为空", trigger: "blur" },
    ],
    specificationModel: [
      { required: true, message: "规格型号不能为空", trigger: "blur" },
    ],
    inventoryTypeId: [
      { required: true, message: "存货类型ID不能为空", trigger: "blur" },
    ],
    inventoryCategoryId: [
      { required: true, message: "存货分类ID不能为空", trigger: "blur" },
    ],
    pricingMethodId: [
      { required: true, message: "计价方式ID不能为空", trigger: "blur" },
    ],
    status: [
      { required: true, message: "0启用 1禁用不能为空", trigger: "change" },
    ],
    isSupportMultipleUnits: [
      {
        required: true,
        message: "是否支持多计量单位不能为空",
        trigger: "blur",
      },
    ],
    unitId: [
      { required: true, message: "计量单位ID不能为空", trigger: "blur" },
    ],
    purchaseUnitId: [
      { required: true, message: "采购单位ID不能为空", trigger: "blur" },
    ],
    salesUnitId: [
      { required: true, message: "销售单位ID不能为空", trigger: "blur" },
    ],
    stockUnitId: [
      { required: true, message: "库存单位ID不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询存货管理列表 */
function getList() {
  loading.value = true;
  listManagement(queryParams.value).then((response) => {
    managementList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    inventoryName: null,
    inventoryNumber: null,
    specificationModel: null,
    inventoryTypeId: null,
    inventoryCategoryId: null,
    pricingMethodId: null,
    status: null,
    productPurpose: null,
    remarks: null,
    isSupportMultipleUnits: null,
    unitId: null,
    purchaseUnitId: null,
    salesUnitId: null,
    stockUnitId: null,
    purchasePrice: null,
    salesPrice: null,
    costPrice: null,
    minStock: null,
    maxStock: null,
    safetyStock: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
  };
  proxy.resetForm("managementRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加存货管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getManagement(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改存货管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["managementRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateManagement(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addManagement(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除存货管理编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delManagement(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "inventory/management/export",
    {
      ...queryParams.value,
    },
    `management_${new Date().getTime()}.xlsx`
  );
}

const inventoryTypeList = ref([]);
const inventoryCategoryList = ref([]);
const pricingMethodList = ref([]);
const inventoryUnitList = ref([]);

function getSelectAll() {
  getSelectAllData().then((response) => {
    inventoryTypeList.value = response.data.inventoryTypeList;
    inventoryCategoryList.value = response.data.inventoryCategoryList;
    pricingMethodList.value = response.data.pricingMethodList;
    inventoryUnitList.value = response.data.inventoryUnitList;
  
  });
}

getSelectAll();
getList();
</script>
